﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Bouyei.Discoverer.Class
{
    class BaseClass
    {
        public BaseClass()
        { }

        public string ToMacAddressFormat(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; ++i)
            {
                sb.Append(bytes[i].ToString("X2") + ":");
            }

            sb = sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }

        public string BytesToHexString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; ++i)
            {
                if (i > 0 && i % 16 == 0)
                {
                    sb.Append(Environment.NewLine);
                }
                sb.Append(bytes[i].ToString("X2") + " ");
            }
            return sb.ToString();
        }

        public string BytesToStringFormat(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; ++i)
            {
                if (i > 0 && (i % 8 == 0))
                {
                    sb.Append(Environment.NewLine);
                }
                sb.Append(bytes[i].ToString() + ",");
            }
            return sb.ToString();
        }

        public string ScanFilter(byte[] srcBytes,byte[] target,int srcIndex=0)
        {
         
            int index = JKMP(srcBytes,target ,srcIndex);
            if(index>=0)
            {
                return Encoding.Default.GetString(srcBytes);
            }
            else
            {
                return string.Empty;
            }
        }

        public string ToString(object[] objects)
        {
            StringBuilder sb = new StringBuilder();
            foreach(object obj in objects)
            {
                sb.Append(obj.ToString()+";");
            }
            return sb.ToString();
        }

        private int JKMP(byte[] srcArray, byte[] targetArray,int srcIndex=0)
        {
            int j = srcIndex, i = 0, pi = -1;
            while (i < srcArray.Length)
            {
                if (srcArray[i] == targetArray[j])
                {
                    if (pi == -1 && j != 0 && targetArray[0] == srcArray[i])
                        pi = i;//记录主串中的字符是否有和子串的第一个字符相同的位置
                    ++i;
                    ++j;
                    if (j >= targetArray.Length) //匹配完成
                        return i - j;
                }
                else
                {
                    if (pi != -1)//字符不相同则移动pi个长度，提高效率的关键部分就是这个地方了。
                    {
                        i = pi + 1;
                        j = 1;
                        pi = -1;
                    }
                    else//和普通的匹配一样，匹配不成功则子串从0开始,主串继续下一个匹配
                    {
                        j = 0;
                        ++i;
                    }
                }
            }
            return -1;
        }
    }
}
